﻿using _12306helper.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace _12306helper.UI
{
    public partial class LoginForm : Form
    {
        private bool isCheckCodeSuc = false;
        private bool isLoginSuc = false;
        public LoginForm()
        {
            InitializeComponent();
        }

        private void bwGetCode_DoWork(object sender, DoWorkEventArgs e)
        {
            using (Stream stream = AuthCodeService.GetLoginCode())
            {
                Image im = Image.FromStream(stream);
                picCode.Image = im;
            }
        }

        private void LoginForm_Load(object sender, EventArgs e)
        {
            if (!bwGetCode.IsBusy) bwGetCode.RunWorkerAsync();
            cleanError();
        }

        private void picCode_Click(object sender, EventArgs e)
        {
            if (!bwGetCode.IsBusy) bwGetCode.RunWorkerAsync();
        }

        private void bwCheckCode_DoWork(object sender, DoWorkEventArgs e)
        {
            isCheckCodeSuc = false;
            string code = txtCode.Text;
            if (string.IsNullOrEmpty(code)) return;
            isCheckCodeSuc = AuthCodeService.CheckCode(code);
        }

        private void txtCode_TextChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtCode.Text) || txtCode.Text.Length < 4) return;
            if (!bwCheckCode.IsBusy) bwCheckCode.RunWorkerAsync();
        }

        private void bwCheckCode_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            cleanError();
            if (!isCheckCodeSuc)
            {
                //MessageBox.Show("验证失败");
                printError("验证码错误");
                txtCode.Text = "";
                //if (!bwGetCode.IsBusy) bwGetCode.RunWorkerAsync();
                txtCode.Focus();
            }
        }
        private void printError(string msg)
        {
            lblError.Text = msg;
        }
        private void cleanError()
        {
            lblError.Text = "";
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtUser.Text))
            {
                printError("用户名不能为空");
                return;
            }
            if (string.IsNullOrEmpty(txtPwd.Text))
            {
                printError("密码不能为空");
                return;
            }
            if (!isCheckCodeSuc)
            {
                printError("验证码错误");
                return;
            }
            if (!bwLogin.IsBusy) bwLogin.RunWorkerAsync();

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void bwLogin_DoWork(object sender, DoWorkEventArgs e)
        {
            isLoginSuc = UserService.Login(txtUser.Text, txtPwd.Text, txtCode.Text);
        }

        private void bwLogin_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            cleanError();
            if (isLoginSuc)
            {
                printError("登陆成功");
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                printError("用户名或者密码错误");
                if (!bwGetCode.IsBusy) bwGetCode.RunWorkerAsync();
            }
        }

        private void bwGetCode_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            txtCode.Text = string.Empty;
            txtCode.Focus();
        }
    }
}
